// import onLogin from "./login.js";

let BASE_URL;
// BASE_URL = "http://localhost:20011"; //zhuyimin本地
// BASE_URL = 'http://192.168.0.17:18084' //hqh
// BASE_URL = 'http://192.168.0.11:18084'//lw
BASE_URL = "http://120.78.177.187:8083"; //线上 
// BASE_URL = "https://jccgce.yocrea.cn"; //测试


export const sendRequest = (options) => {
	uni.request({
		url: BASE_URL + options.url,
		method: options.method || "GET",
		header: options.header,
		header: {
			Authorization: uni.getStorageSync("yljjToken")
			// Authorization: '123456'
		},
		data: options.data || {},
		TimeOut: 3000,
		success: (res) => {
			if (res.data.code == 0 || res.data.code == 200) {
				//options.success(res.data.data);
				uni.removeStorageSync("res")
				options.success(res);
			} else if (res.data.msg == "该店铺已有企业入驻") {
				options.success(res);
			} else if (res.data.msg == "验证码错误") {
				options.success(res);
			} else if (res.data.code == 75) {
				uni.showToast({
					title: res.data.msg || "服务器错误~",
					icon: "none",
				});
			} else if (res.data.code == 201) {
				options.success(res);
			} else if (res.data.code == 4005) {
				getToken();
			} else if (res.data.code == 202) {
				uni.removeStorageSync("fcwToken")
				uni.removeStorageSync("userInfo")
				uni.navigateTo({
					url: '/pages/myMain/change'
				})
			} else {
				if (res.data.data) {
					uni.showToast({
						title: res.data.data || "服务器错误~",
						icon: "none",
					});
				}
			}
		},
		fail: (err) => {
			uni.showToast({
				title: "网络错误",
				icon: "none",
			});
		},
	});
};

export const sendRequests = (options) => {
	uni.request({
		url: BASE_URL + options.url,
		method: options.method || "GET",
		header: options.header,
		data: options.data || {},
		TimeOut: 3000,
		success: (res) => {
			if (res.data.code == 0 || res.data.code == 20000) {
				options.success(res);
			} else if (res.data.code == 4005) {
				getToken();
			} else {
				options.success(res);
			}
		},
		fail: (err) => {
			uni.showToast({
				title: "网络错误",
				icon: "none",
			});
		},
	});
};

const getToken = () => {

	//#ifdef H5
	let code = getUrlParam("code");
	if (code) {
		getUserInfo();
	}

	// #endif

	if (!uni.getStorageSync("userInfo")) {
		checkLogin();
	}
	// else if (!uni.getStorageSync("userInfo").phone) {
	// let datas = getRoute(1);
	// if (datas.from == "tabBar") {
	// 	uni.navigateTo({
	// 		url: "/pages/enter/bindPhone",
	// 	});
	// } else {
	// 	uni.navigateTo({
	// 		url: `/pages/enter/bindPhone?redirect=/${datas.url}`,
	// 	});
	// }
	// } 
	else {
		sendRequest({
			url: "/wx/token/create",
			method: "post",
			data: {
				phoneNum: uni.getStorageSync("userInfo").phone,
				userId: uni.getStorageSync("userInfo").id,
			},
			success: (res) => {
				if (res.data.code == 20000) {
					uni.setStorageSync("fcwToken", res.data.msg);
				}
			},
		});
	}
};

const getUserInfo = () => {
	let code = getUrlParam("code");
	sendRequest({
		url: "/wx/dealer/wxLogin",
		data: {
			code
		},
		success: (res) => {
			if (res.data.code == 0) {
				uni.setStorageSync("userInfo", res.data.data);
				uni.setStorageSync("wxFcwH5Openid", res.data.data.wxFcwH5Openid);
			}
		},
	});
}



//获取h5链接参数
const getUrlParam = (name) => {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	let url = window.location.href.split("#")[0];
	let search = url.split("?")[1];
	if (search) {
		var r = search.substr(0).match(reg);
		if (r !== null) return unescape(r[2]);
		return null;
	} else {
		return null;
	}
}

const checkLogin = () => {
	let openid = uni.getStorageSync("wxFcwH5Openid");
	// let url = encodeURIComponent("https://www.nkjj88.com/mobile");
	// let url = encodeURIComponent("https://jccgce.yocrea.cn/mobile");
	let appid = "wx5bd3b047396a1b6d";

	//#ifdef MP-WEIXIN
	let link = getRoute(1);
	if (link.from == "other") {
		uni.setStorageSync("redirect", link.url);
	}
	uni.showModal({
		title: "提示",
		content: "您还未登录!",
		showCancel: false,
		confirmText: "前往登录",
		success: function(res) {
			if (res.confirm) {
				uni.switchTab({
					url: "/pages/myMain/myMain",
				});
			}
		},
	});
	// #endif

	// #ifdef H5
	let route_link = getRoute();
	let url = encodeURIComponent(route_link.url);
	if (!openid) {
		var ua = navigator.userAgent.toLowerCase();
		var isWeixin = ua.indexOf('micromessenger') != -1;
		if (isWeixin) {
			window.location.href =
				`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
		}
	} else {
		let navtoLink = getRoute(1);
		if (navtoLink.from == "other") {
			uni.navigateTo({
				url: `/pages/enter/bindPhone?redirect=/${navtoLink.url}`,
			});
		} else {
			uni.navigateTo({
				url: "/pages/enter/bindPhone",
			});
		}
	}
	// #endif
};

//获取路由
const getRoute = (type) => {
	let url = `${BASE_URL}/mobile`;
	let routes = getCurrentPages();
	let curRoute = routes[routes.length - 1].route;
	let tabBarRoute = ["pages/index/index", "pages/let/index", "pages/message/message", "pages/myMain/myMain"]
	if (tabBarRoute.indexOf(curRoute) != -1) {
		return {
			url,
			from: "tabBar"
		}
	} else {
		let params = getRouteParam();
		let redirect_url = `${url}/#/${curRoute}?${params}`;
		return {
			url: type ? `/${curRoute}?${params}` : redirect_url,
			from: "other"
		}
	}
}

//获取路由参数
const getRouteParam = () => {
	let routes = getCurrentPages();
	let curParam = routes[routes.length - 1].options;
	let param = "";
	for (let key in curParam) {
		param += '&' + key + '=' + curParam[key];
	}
	return param;
}

//图片上传
export const upload = (options) => {
	let apiUrl = `${BASE_URL}/wx/upload/uploadFile`;
	uni.uploadFile({
		url: options.url ? `${BASE_URL}${options.url}` : apiUrl,
		method: "post",
		header: {
			"content-type": "multipart/form-data",
		},
		formData: {
			type: "image",
		},
		filePath: options.data,
		name: "file",
		success: (res) => {
			res = JSON.parse(res.data) || res;
			if (res.code == 0 || res.code == 20000) {
				options.success(res.data.url);
			} else {
				uni.showToast({
					title: res.msg,
					icon: "none",
				});
			}
		},
		fail: (err) => {
			uni.showToast({
				title: "网络错误",
			});
			// reject(err)
		},
	});
};


//视频间url
export const getCamareUrl = (options) => {
	let url = 'https://cgm.nkjj88.com/camera/#/'
	// let url = 'http://192.168.0.7:8080/#/'
	return url
};


export const getUrl = BASE_URL;
